#include "userwindow.h"



UserWindow::UserWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::UserWindow)
{
  ui->setupUi(this);
  addNewInfo = new AddInfo;
  addNewInfoSup = new addInfoSup;
  Modify *mod = new Modify;
  own = new Owner;
  Sup = new Supervisor;
  staf = new Staff;
  Vol = new Volunteer;

// 直接设置主管和志愿者信息
  SpAccount = "0001";
  SpPassword = "111111";
  SpId = "光谷社区";
  VolAccount = "0002";
  VolPassword = "222222";
  ui->labelCom->setText(SpId);

  //连接各信号和槽函数

  //用户和主管保存更改的信息
  connect(mod, &Modify::saveModify, [=](){
    int a = tableIndex_.index;
    QList<QStringList> *Table = new QList<QStringList>;
    qDebug() << a;
    QString newvalue = mod->modifyValue_;
    switch (a) {
     case SpOwner: //主管修改业主表
      Sup->ModifyOwner(tableIndex_.row, tableIndex_.column, newvalue);
      Table = Sup->GetAllOwners();

      UpdateTable(Table, ui->tableOwnerM, 0, 3);

      break;
     case SpStaff:
      //主管修改员工表
      Sup->ModifyStaff(tableIndex_.row, tableIndex_.column, newvalue);
      Table = Sup->GetAllStaffs();
      if (!Table->isEmpty()){
         UpdateTable(Table, ui->tableStaff, 0, 2);
      }
       break;
     case SpIO:
      //主管修改进出表
      Sup->ModifyIOinfo(tableIndex_.row, tableIndex_.column, newvalue);
      Table = Sup->GetAllIOs();
      if (!Table->isEmpty()){
        UpdateTable(Table, ui->tableIOinfoM, 0, 0);
      }
       break;
     case StafIO:
      //物业工作人员修改进出表
      staf->ModifyIOinfo(tableIndex_.row, tableIndex_.column, newvalue);
      Table = staf->GetAllIOs();
      if (!Table->isEmpty()){
        UpdateTable(Table, ui->tableIOinfoS, 0, 1);
      }
      break;
     case OwnMem:
      //业主修改成员表
       own->ModifyMember(tableIndex_.row, tableIndex_.column, newvalue);
       Table = own->GetAllMembers();
       if (!Table->isEmpty()){
         UpdateTable(Table, ui->tableFamily, 3, 0);
       }
       break;
     case VolUna:
      //志愿者修改到达时间
       Vol->ModifyBuyTime(newvalue, tableIndex_.row);
       Table = Vol->GetArrived();
       if (!Table->isEmpty()){
         UpdateTable(Table, ui->tableBuyV, 0, 1);
       }
       Table = Vol->GetUnArrived();
       ui->tableBuyVUA->removeRow(tableIndex_.row);
//       if (!Table->isEmpty()){
//         UpdateTable(Table, ui->tableBuyVUA, 0, 1);
//       }
       break;
    default:
      break;
    }
  });

  //用户添加信息按钮
  connect(ui->ButtonOnwAdd, &QPushButton::clicked, [=](){
    addNewInfo->tabPage_ = ui->tabWidgetO->currentIndex();
    addNewInfo->show_window();

  });

  //用户更改信息按钮
  connect(ui->ButtonOnwMod, &QPushButton::clicked, [=](){
    QTableWidget *Table = new QTableWidget;
    if (ui->tabWidgetO->currentIndex()==0) {  //当前索引为家庭信息
      Table = ui->tableFamily;
      tableIndex_.index = OwnMem;
      //这里获取到当前的行，对表项信息进行修改
      tableIndex_.row = Table->currentRow();
      tableIndex_.column = Table->currentColumn();
      if (tableIndex_.column==0)
        return ;
      mod->show();
    }
  });

  //用户删除信息按钮
  connect(ui->ButtonOwnDel, &QPushButton::clicked,[=](){
    QTableWidget *Table = new QTableWidget;
    // 获取当前表格的行
    int currentRow = Table->currentRow();
    if (ui->tabWidgetO->currentIndex()==0) {  //当前索引为家庭信息
      Table = ui->tableFamily;
      currentRow = Table->currentRow();
      QString nowname = ui->tableFamily->item(currentRow, 0)->text();

      own->DeleteMember(nowname);
    } else {
      Table = ui->tableBuyO;
      currentRow = Table->currentRow();
      QString nowname = ui->tableBuyO->item(currentRow, 0)->text();
      QString nowdate = ui->tableBuyO->item(currentRow, 2)->text();
      own->DeleteBuy(nowname, nowdate);
    }
    //删除此行
    Table->removeRow(Table->currentRow());

  });

  //用户保存
  connect(addNewInfo, &AddInfo::Save, [=](){

    if (ui->tabWidgetO->currentIndex()==0) {  //当前索引为家庭信息

      //这里保存家庭信息
      own->AddMember(addNewInfo->Person);
      //添加新行，更新显示
      QList<QStringList> *Family = own->GetAllMembers();

      if (!Family->isEmpty()){
        UpdateTable(Family, ui->tableFamily, 3, 0);
      }

    } else {    //当前为购物信息

      //这里保存购物信息，添加其他信息
      own->AddBuy(addNewInfo->Buy);
      //获取当前行，在最下方增加一行,更新显示
      QList<QStringList> *Buy = own->GetAllBuys();

      if (!Buy->isEmpty()){
        UpdateTable(Buy, ui->tableBuyO, 4, 0);
      }
    }
  });

  //主管添加信息按钮
  connect(ui->ButtonSpAdd, &QPushButton::clicked, [=](){

    addNewInfoSup->tabpage_ = ui->tabWidgetM->currentIndex();
    addNewInfoSup->show_window();
  });

  //主管和工作人员保存信息
  connect(addNewInfoSup, &addInfoSup::Save, [=](){
    if (userIndex_ == kSupevisor){  //主管界面
       int currentIndex = ui->tabWidgetM->currentIndex();
       if (currentIndex==2) {  //当前索引为出入信息
         //保存信息
         Sup->AddIOinfo(addNewInfoSup->IO);
         QList<QStringList> *IOs = Sup->GetAllIOs();
         if (!IOs->isEmpty()){
           UpdateTable(IOs, ui->tableIOinfoM, 0, 0);
         }
       } else if (currentIndex==1) { //工作人员信息
         //保存信息
         Sup->AddStaff(addNewInfoSup->Person);
         QList<QStringList> *Staffs = Sup->GetAllStaffs();
         if (!Staffs->isEmpty()){
            UpdateTable(Staffs, ui->tableStaff, 0, 2);
         }
        } else {  //业主信息
         //保存信息
         Sup->AddOwner(addNewInfoSup->Person);
          QList<QStringList> *Owners = Sup->GetAllOwners();
         if (!Owners->isEmpty()){
           UpdateTable(Owners, ui->tableOwnerM, 0, 4);
         }
        }
    } else if (userIndex_ == kStaff) {  //工作人员界面
        staf->AddIOinfo(addNewInfoSup->IO);
        QList<QStringList> *IOs = staf->GetAllIOs();
        if (!IOs->isEmpty()){
          UpdateTable(IOs, ui->tableIOinfoS, 0, 1);
        }
    }
  });

  //主管更改信息按钮
  connect(ui->ButtonSpMod, &QPushButton::clicked, [=](){
    QTableWidget *Table = new QTableWidget;
    int currentIndex = ui->tabWidgetM->currentIndex();
    if (currentIndex==2) {  //当前索引为出入信息
      Table = ui->tableIOinfoM;
      tableIndex_.index = SpIO;
    } else if (currentIndex==1) { //工作人员信息
      Table = ui->tableStaff;
      tableIndex_.index = SpStaff;
    } else {
      Table = ui->tableOwnerM;  //业主信息
      tableIndex_.index = SpOwner;
    }
    //这里获取到当前的行，对表项信息进行修改
    tableIndex_.row = Table->currentRow();
    tableIndex_.column = Table->currentColumn();
    if (tableIndex_.column < 3 && tableIndex_.index==SpOwner)
      return ;
    if (tableIndex_.column == 1 && tableIndex_.index==SpStaff)
      return ;
    if ((tableIndex_.column<4 && tableIndex_.column!=1)&& tableIndex_.index==SpIO)
      return ;
    mod->show();
  });

  //主管删除信息按钮
  connect(ui->ButtonSpDel, &QPushButton::clicked,[=](){
    QTableWidget *Table = new QTableWidget;
    int currentIndex = ui->tabWidgetM->currentIndex();
    int currentRow = 0;
    if (currentIndex==2) {  //当前索引为出入信息
      Table = ui->tableIOinfoM;
      currentRow = Table->currentRow();
      QString nowdate = Table->item(currentRow, 0)->text();
      QString nowtel = Table->item(currentRow, 2)->text();
      QString nowintime = Table->item(currentRow, 3)->text();
      Sup->DeleteIOinfo(nowdate, nowtel, nowintime);
    } else if (currentIndex==1) { //工作人员信息
      Table = ui->tableStaff;
      currentRow = Table->currentRow();
      QString nowname = Table->item(currentRow, 1)->text();
      Sup->DeleteStaff(nowname);
    } else {
      Table = ui->tableOwnerM;  //业主信息
      currentRow = Table->currentRow();
      QString nowname = Table->item(currentRow, 2)->text();
      Sup->DeleteOwner(nowname);
    }

    //删除此行
    Table->removeRow(Table->currentRow());
  });

  //工作人员添加出入信息
  connect(ui->pushButtonAddS, &QPushButton::clicked, [=](){
    if(ui->tabWidgetS->currentIndex() == 2){
      addNewInfoSup->tabpage_ = 2;
      addNewInfoSup->show_window();
    }
  });

  //工作人员删除出入信息
  connect(ui->pushButtonDelS, &QPushButton::clicked, [=](){
    if (ui->tabWidgetS->currentIndex() == 2) {  //出入信息界面
      QTableWidget *Table = new QTableWidget;
      int currentRow = 0;
      Table = ui->tableIOinfoS;
      currentRow = Table->currentRow();
      QString nowdate = Table->item(currentRow, 0)->text();
      QString nowtel = Table->item(currentRow, 2)->text();
      QString nowintime = Table->item(currentRow, 3)->text();
      staf->DeleteIOinfo(nowdate, nowtel, nowintime);
      Table->removeRow(Table->currentRow());
    }
  });

  //工作人员修改出入信息
  connect(ui->pushButtonIOS, &QPushButton::clicked, [=](){
    if (ui->tabWidgetS->currentIndex() == 2) {  //出入信息界面
      tableIndex_.index = StafIO;
      tableIndex_.row = ui->tableIOinfoS->currentRow();
      tableIndex_.column = ui->tableIOinfoS->currentColumn();
      if ((tableIndex_.column<4 && tableIndex_.column!=1)&& tableIndex_.index==StafIO)
        return ;
      mod->show();
    }
  });

  //点击退出,本窗口关闭,释放登出信号
  connect(ui->actionqiut, &QAction::triggered, [=](){
    emit Logout();
  });

  //志愿者修改到达时间
  connect(ui->pushButtonModV, &QPushButton::clicked, [=](){
    if (ui->tabWidgetVA->currentIndex() == 1){ //修改的是未送达
      tableIndex_.index = VolUna;
      tableIndex_.row = ui->tableBuyVUA->currentRow();
      if (tableIndex_.row < 0)
        return ;
      else
        mod->show();
    }
  });

}

//登陆槽函数
void UserWindow::Log(const info& id) {
  if (userIndex_ == kOwner) {
    own->GetID(id);
    SetOwnerTable();  //配置业主界面
    ui->labelBuidO->setText(id.buildingid);
    ui->label_username->setText(id.name);
    ui->label_address->setText(id.address);
  } else if (userIndex_ == kStaff) {
    staf->GetID(id);
    SetStaffTable(); //配置工作人员界面
    ui->labelSid->setText(id.buildingid);
    ui->labelSName->setText(id.name);
  } else if (userIndex_ == kSupevisor) {
    SetSupvTable();   //配置主管界面
  } else if (userIndex_ == kVolunt) {
    SetVoluntTable();  //配置志愿者界面
  }
  ui->stackedWidget->setCurrentIndex(userIndex_ - 1);
  this->show();
}

//显示表项目
void UserWindow::UpdateTable(QList<QStringList> *Family, QTableWidget *table,
                             int start, int end) {

  int row = Family->size();
  int column = Family->first().size();
  table->setRowCount(row);
  for (int i=0; i<row; i++){
    for (int j=start; j<column-end; j++){
      table->setItem(i, j-start, new QTableWidgetItem(Family->at(i).at(j)));
    }

  }
}

//配置业主界面
void UserWindow::SetOwnerTable() {

  //家庭信息显示
  //设置列数
  ui->tableFamily->setColumnCount(6);
  //设置水平表头
  QStringList horizonHeader;
  horizonHeader << "姓名" << "性别" << "年龄" << "联系方式" << "体温(℃)" << "健康状态";
  ui->tableFamily->setHorizontalHeaderLabels(horizonHeader);


  //获取家庭信息
  QList<QStringList> *Family = own->GetAllMembers();
  if (!Family->isEmpty()){
    UpdateTable(Family, ui->tableFamily, 3, 0);
  }
  //购物信息显示
  //设置列数
  ui->tableBuyO->setColumnCount(5);
  //设置水平表头
  QStringList horizonHeader1;
  horizonHeader1 << "物品" << "数量" << "下单时间" << "送达时间" << "状态";
  ui->tableBuyO->setHorizontalHeaderLabels(horizonHeader1);
  QList<QStringList> *Buy = own->GetAllBuys();
  if (!Buy->isEmpty()){
    UpdateTable(Buy, ui->tableBuyO, 4, 0);
  }
  //delete Buy;
  //设置正文
  //ui->tableInfo->setItem();

}

//配置物业主管界面
void UserWindow::SetSupvTable() {

  //业主信息显示
  //设置列数
  ui->tableOwnerM->setColumnCount(7);
  //设置水平表头
  QStringList headerOnw;
  headerOnw << "楼栋号" << "门牌号" << "姓名" << "性别" << "年龄" << "联系方式" << "健康状态";
  ui->tableOwnerM->setHorizontalHeaderLabels(headerOnw);
  QList<QStringList> *Owners = Sup->GetAllOwners();
  if (!Owners->isEmpty()){
    UpdateTable(Owners, ui->tableOwnerM, 0, 3);
  }

  //工作人员信息显示
  ui->tableStaff->setColumnCount(7); //设置列数
  //设置水平表头
  QStringList headerStaff;
  headerStaff << "负责楼栋" << "姓名" << "性别" << "年龄" << "联系方式" << "体温(℃)" << "健康状态";
  ui->tableStaff->setHorizontalHeaderLabels(headerStaff);
  QList<QStringList> *Staffs = Sup->GetAllStaffs();
  if (!Staffs->isEmpty()){
    UpdateTable(Staffs, ui->tableStaff, 0, 2);
  }

  //出入信息显示
  //设置列数
  ui->tableIOinfoM->setColumnCount(7);
  //设置水平表头
  QStringList headerIO;
  headerIO << "日期" << "姓名" << "联系方式" << "进入时间" << "离开时间" << "体温(℃)" <<"楼栋号";
  ui->tableIOinfoM->setHorizontalHeaderLabels(headerIO);

  QList<QStringList> *IOs = Sup->GetAllIOs();
  if (!IOs->isEmpty()){
    UpdateTable(IOs, ui->tableIOinfoM, 0, 0);
  }

}

// 物业工作人员界面
void UserWindow::SetStaffTable() {
  //居住人员信息显示
  //设置列数
  ui->tableResident->setColumnCount(8);
  //设置水平表头
  QStringList horizonHeader;
  horizonHeader << "门牌号" << "业主" << "姓名" << "性别" << "年龄" << "联系方式" << "体温(℃)" << "健康状态";
  ui->tableResident->setHorizontalHeaderLabels(horizonHeader);
  QList<QStringList> *residents = staf->GetAllResidents();
  if (!residents->isEmpty()){
    UpdateTable(residents, ui->tableResident, 1, 0);
  }


  //业主信息显示
  //设置列数
  ui->tableOwnerS->setColumnCount(7);
  //设置水平表头
  horizonHeader.clear();
  horizonHeader << "门牌号" << "姓名" << "性别" << "年龄" << "联系方式" << "体温(℃)" << "健康状态";
  ui->tableOwnerS->setHorizontalHeaderLabels(horizonHeader);
  QList<QStringList> *owners = staf->GetAllOwners();
  if (!owners->isEmpty()){
    UpdateTable(owners, ui->tableOwnerS, 1, 2);
  }


  //出入信息显示
  //设置列数
  ui->tableIOinfoS->setColumnCount(6);
  //设置水平表头
  QStringList headerIO;
  headerIO << "日期" << "姓名" << "联系方式" << "进入时间" << "离开时间" << "体温(℃)";
  ui->tableIOinfoS->setHorizontalHeaderLabels(headerIO);
  QList<QStringList> *IOs = staf->GetAllIOs();
  if (!IOs->isEmpty()){
    UpdateTable(IOs, ui->tableIOinfoS, 0, 1);
  }

}

// 志愿者界面
void UserWindow::SetVoluntTable() {

  QStringList horizonHeader;
  //已送达购物信息显示
  //设置列数
  ui->tableBuyV->setColumnCount(8);
  //设置水平表头
  horizonHeader << "楼栋号" << "门牌号" << "姓名" << "联系方式" << "物品"
                << "数量" << "下单时间" << "送达时间";
  ui->tableBuyV->setHorizontalHeaderLabels(horizonHeader);
  QList<QStringList> *buyar = Vol->GetArrived();
  if (!buyar->isEmpty()){
    UpdateTable(buyar, ui->tableBuyV, 0, 1);
  }

  //未送达购物信息显示
  //设置列数
  ui->tableBuyVUA->setColumnCount(8);
  //设置水平表头
  horizonHeader << "楼栋号" << "门牌号" << "姓名" << "联系方式" << "物品"
                << "数量" << "下单时间" << "送达时间";
  ui->tableBuyVUA->setHorizontalHeaderLabels(horizonHeader);
  QList<QStringList> *buyuar = Vol->GetUnArrived();
  if (!buyuar->isEmpty()){
    UpdateTable(buyuar, ui->tableBuyVUA, 0, 1);
  }



}


UserWindow::~UserWindow()
{

  delete ui;
}
